package com.common.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

import org.openqa.selenium.WebDriver;

public class PageBase {
	
	UITestBase tb;

    public PageBase(UITestBase tb){
        this.tb = tb;
        elementinit();
    }

    /**
     * 利用反射得到DefaultElement
     */
    public void elementinit(){
        try {
            Class<?> rt_class = this.getClass();
            Field[] fields = rt_class.getDeclaredFields();
            for(Field field : fields){
                if(field.getType()==com.common.test.DefaultElement.class || field.getType().getSuperclass()==com.common.test.DefaultElement.class){
                    if(field.isAnnotationPresent(FindElement.class)){
                        FindElement findelement = field.getAnnotation(FindElement.class);
                        Class<?> fo_class = (Class<?>) Class.forName(field.getType().getName());
                        WebDriver driver = tb.getDriver();
                        Constructor<?> constructor = fo_class.getDeclaredConstructor(new Class[]{WebDriver.class, String.class, String.class, String.class});
                        field.setAccessible(true);
                        field.set(this, constructor.newInstance(new Object[]{driver, findelement.findName(), findelement.findValue(), findelement.desc()}));
                    }
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
